home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / Peter Lewis (TCPExample) / PNL Libraries / MyStartup.p < prev    next >
Encoding:
Text File  |  1995-11-06  |  4.2 KB  |  194 lines  |  [TEXT/CWIE]

  1. unit MyStartup;
  2.  
  3. interface
  4.  
  5.     uses
  6.         Types;
  7.     
  8.     var
  9.         current_time: longint;
  10.         
  11.     type
  12.         StartupMessages = (SMT_None, SMT_Startup, SMT_Generic, SMT_Last);
  13.         StartupInitProc = function(var msg: integer): OSStatus;
  14.         StartupIdleProc = procedure;
  15.         StartupFinishProc = procedure;
  16.         
  17.     procedure InitStartup;
  18.     procedure SetStartup(init: StartupInitProc; idle: StartupIdleProc; idle_period: longint; finish: StartupFinishProc);
  19.     function Startup(var msg: integer): OSStatus;
  20.     procedure IdleStartup;
  21.     procedure FinishStartup;
  22.     procedure FireAtIdle(idle: StartupIdleProc);
  23.     
  24. implementation
  25.  
  26.     uses
  27.         Memory, Events, MyMemory;
  28.     
  29.     type
  30.         EntryRecord = record
  31.             init: StartupInitProc;
  32.             idle: StartupIdleProc;
  33.             idle_period: longint;
  34.             next_idle: longint;
  35.             finish: StartupFinishProc;
  36.         end;
  37.         EntryArray = array[1..10000] of EntryRecord;
  38.         EntryPtr = ^EntryArray;
  39.         EntryHandle = ^EntryPtr;
  40.  
  41.     const
  42.         idles_max = 100;
  43.         
  44.     var
  45.         idles: array[1..idles_max] of StartupIdleProc;
  46.         max_idles: longint;
  47.         startup_error: OSStatus;
  48.         entries: EntryHandle;
  49.         entries_count: longint;
  50.     
  51.     procedure FireAtIdle(idle: StartupIdleProc);
  52.         var
  53.             i: longint;
  54.             found: Boolean;
  55.             hack: StartupIdleProc;
  56.     begin
  57.         found := false;
  58.         for i := 1 to idles_max do begin
  59.             hack := idles[i];
  60.             if hack= nil then begin
  61.                 idles[i] := idle;
  62.                 if i > max_idles then begin
  63.                     max_idles := i;
  64.                 end;
  65.                 found := true;
  66.                 leave;
  67.             end;
  68.         end;
  69.         if not found then begin
  70.             idle();
  71.         end;
  72.     end;
  73.     
  74.     procedure SetStartup(init: StartupInitProc; idle: StartupIdleProc; idle_period: longint; finish: StartupFinishProc);
  75.         var
  76.             found: Boolean;
  77.             entry: EntryRecord;
  78.             i: longint;
  79.     begin
  80.         if (startup_error = noErr) & (entries = nil) then begin
  81.             startup_error := MNewHandle(entries, 0);
  82.         end;
  83.         if startup_error = noErr then begin
  84.             found := false;
  85.             for i := 1 to entries_count do begin
  86.                 if (entries^^[i].init = init) & (entries^^[i].idle = idle) & (entries^^[i].idle_period = idle_period) & (entries^^[i].finish = finish) then begin
  87.                     found := true;
  88.                     leave;
  89.                 end;
  90.             end;
  91.             if not found then begin
  92.                 entry.init := init;
  93.                 entry.idle := idle;
  94.                 entry.idle_period := idle_period;
  95.                 entry.next_idle := TickCount;
  96.                 entry.finish := finish;
  97.                 startup_error := PtrAndHand(@entry, Handle(entries), SizeOf(entry));
  98.                 if startup_error = noErr then begin
  99.                     Inc(entries_count);
  100.                 end;
  101.             end;
  102.         end;
  103.     end;
  104.     
  105.     procedure IdleStartup;
  106.         var
  107.             i: longint;
  108.             tmp_hack: StartupIdleProc;
  109.     begin
  110.         current_time := TickCount;
  111.         for i := 1 to entries_count do begin
  112.             tmp_hack := entries^^[i].idle;
  113.             if (tmp_hack <> nil) & (current_time >= entries^^[i].next_idle) then begin
  114.                 entries^^[i].next_idle := current_time + entries^^[i].idle_period;
  115.                 tmp_hack;
  116.             end;
  117.         end;
  118.         for i := 1 to max_idles do begin
  119.             tmp_hack := idles[i];
  120.             if (tmp_hack<> nil) then begin
  121.                 tmp_hack();
  122.                 idles[i] := nil;
  123.             end;
  124.         end;
  125.     end;
  126.     
  127.     procedure InitStartup;
  128.         var
  129.             i: longint;
  130.     begin
  131.         entries := nil;
  132.         entries_count := 0;
  133.         startup_error := noErr;
  134.         max_idles := 0;
  135.         for i := 1 to idles_max do begin
  136.             idles[i] := nil;
  137.         end;
  138.     end;
  139.     
  140.     function Startup(var msg: integer): OSStatus;
  141.         var
  142.             i: longint;
  143.             tmp_hack: StartupFinishProc;
  144.             tmp_hack_init: StartupInitProc;
  145.     begin
  146.         msg := ord(SMT_Startup);
  147.         i := 0;
  148.         while (startup_error = noErr) & (i < entries_count) do begin
  149.             i := i + 1;
  150.             tmp_hack_init := entries^^[i].init;
  151.             if tmp_hack_init <> nil then begin
  152.                 msg := ord(SMT_Generic);
  153.                 startup_error := tmp_hack_init(msg);
  154.             end;
  155.         end;
  156.         if startup_error <> noErr then begin
  157.             i := i - 1;
  158.             while i > 0 do begin
  159.                 tmp_hack := entries^^[i].finish;
  160.                 if tmp_hack <> nil then begin
  161.                     tmp_hack;
  162.                 end;
  163.                 i := i - 1;
  164.             end;
  165.             MDisposeHandle(entries);
  166.             entries_count := 0;
  167.         end;
  168.         if startup_error = noErr then begin
  169.             msg := ord(SMT_None);
  170.         end;
  171.         Startup := startup_error;
  172.     end;
  173.     
  174.     procedure FinishStartup;
  175.         var
  176.             i: longint;
  177.             tmp_hack: StartupFinishProc;
  178.     begin
  179.         if entries <> nil then begin
  180.             i := entries_count;
  181.             while i > 0 do begin
  182.                 tmp_hack := entries^^[i].finish;
  183.                 if tmp_hack <> nil then begin
  184.                     tmp_hack;
  185.                 end;
  186.                 i := i - 1;
  187.             end;
  188.             MDisposeHandle(entries);
  189.             entries_count := 0;
  190.         end;
  191.     end;
  192.  
  193. end.
  194.